#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN MVC MODULE FOR THN WEB SERVER

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For use in commercial software, please obtain a commercial license from THN Solutions LLC.    

  + For use in free software, this library is licensed under GPL version 3. A summary of 
	GPLv3 is listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt 
	included with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 18-Apr-13 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
using Thn.Net.Http;
//using Thn.Serialization.Json;
#endregion

namespace Thn.Web.Mvc
{
    /// <summary>
    /// This class provides basic implementation of <see cref="IViewController"/>.
    /// </summary>
#if DEBUG || TESTING
    public
#else
    public
#endif
 class Controller : IViewController
    {
        #region Fields
        static string JsonMimeType = "application/json";
        #endregion

        #region Properties

        #region Http Context
        private IHttpRequestHandlingContext mHttpContext;
        /// <summary>
        /// Gets or sets the current HTTP request context.
        /// </summary>
        public IHttpRequestHandlingContext HttpContext
        {
            get { return mHttpContext; }
            set { mHttpContext = value; }
        }
        #endregion

        #region View Renderer
        private IViewRenderer mViewRenderer;
        /// <summary>
        /// Gets or sets reference to the renderer responsible for rendering views.
        /// </summary>
        public IViewRenderer ViewRenderer
        {
            get { return mViewRenderer; }
            set { mViewRenderer = value; }
        }
        #endregion
        
        #endregion

        #region Render View
        /// <summary>
        /// Render a view template with the provided data model.
        /// </summary>
        /// <param name="viewName">Name of the view to be rendered.</param>
        /// <param name="model">The data model object to be used by the view.</param>
        protected object RenderView(string viewName, object model = null)
        {
            HttpResponse result = new HttpResponse();
            RenderArgs args = new RenderArgs();
            args.ViewName = viewName;
            args.Model = model;
            args.Writer = result.Body.Writer;
            args.Context = mHttpContext;
            mViewRenderer.Render(args);
            return result;
        }
        #endregion

        #region Reply JSON
        /*
        protected object Json(object graph)
        {
            //initialize if necessary
            if (mJson == null) mJson = new JsonObjectWriter();

            //build response and return it
            HttpResponse result = new HttpResponse();
            mJson.Context = result.Body.Writer;
            mJson.WriteObject(graph);
            result.ContentType = JsonMimeType;

            return result;
        }

        JsonObjectWriter mJson;
        */
        #endregion
        
        #region Constructors
        /// <summary>
        /// Create a new instance.
        /// </summary>
        public Controller()
            : base()
        { }
        #endregion
    }
}
